package org.mspring.platform.dao;

import java.io.Serializable;

/**
 * @author Gao Youbo
 * @since Mar 19, 2012
 */
public class Order implements Serializable {

    private static final long serialVersionUID = -4822253915224902889L;

    private static final String SORT_ASC = "ASC";
    private static final String SORT_DESC = "DESC";

    public enum OrderType {
        ASC(SORT_ASC), DESC(SORT_DESC);

        private String value;

        OrderType(String order) {
            this.value = order;
        }

        @Override
        public String toString() {
            return this.value;
        }
    }

    private String field;

    private OrderType orderType;

    public Order() {
    }

    public Order(String field) {
        this.orderType = OrderType.DESC;
        this.setField(field);
    }

    public Order(String field, OrderType orderType) {
        this.setField(field);
        this.setOrderType(orderType);
    }

    public String getField() {
        return field;
    }

    public void setField(String field) {
        this.field = org.apache.commons.lang3.StringUtils.isBlank(field) ? "" : field;
    }

    public OrderType getOrderType() {
        return orderType;
    }

    public void setOrderType(OrderType orderType) {
        this.orderType = orderType;
    }

    public static Order desc(String field) {
        return new Order(field, OrderType.DESC);
    }

    public static Order asc(String field) {
        return new Order(field, OrderType.ASC);
    }

    public boolean isAsc() {
        return OrderType.ASC.equals(this.orderType);
    }

    /**
     * 重写toString方法.将排序对象转换成一个JSON
     */
    @Override
    public String toString() {
        return "{'field':'" + this.field + "','orderType':'" + this.orderType.toString() + "'}";
    }
}
